package at.jku.mc.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

//TODO does work, but count-mechanism is wrong
public final class UserProvider {
	private static final String FIVETASKSDONE_BADGE = "Five Tasks Done";

	// Database fields
	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;

	public UserProvider(Context context) {
		dbHelper = new DatabaseHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		if (!database.isReadOnly()) {
			// Enable foreign key constraints
			database.execSQL("PRAGMA foreign_keys=ON;");
		}
	}

	private void createUser() {
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.USER_COLUMN_NAME, "Test");
		values.put(DatabaseHelper.USER_COLUMN_BADGES, 0);
		values.put(DatabaseHelper.USER_COLUMN_POINTS, 0);
		long insertId = database
				.insert(DatabaseHelper.TABLE_USER, null, values);
		Cursor cursor = database.query(DatabaseHelper.TABLE_USER, null,
				DatabaseHelper.USER_COLUMN_ID + " = " + insertId, null, null,
				null, null);
		cursor.moveToFirst();
		cursor.close();
	}

	public void close() {
		dbHelper.close();
	}

	public void addPoints(int newPoints) {
		int userpoints = 0;
		Cursor cursor = database.query(DatabaseHelper.TABLE_USER,
				new String[] { DatabaseHelper.USER_COLUMN_POINTS }, null, null,
				null, null, null);
		Log.d("asdf", "usercursor " + cursor);
		if (cursor.getCount() == 0) {
			createUser();
			cursor = database.query(DatabaseHelper.TABLE_USER,
					new String[] { DatabaseHelper.USER_COLUMN_POINTS }, null,
					null, null, null, null);
		}
		if (cursor.moveToFirst())
			userpoints = cursor.getInt(cursor
					.getColumnIndex(DatabaseHelper.USER_COLUMN_POINTS));
		ContentValues args = new ContentValues();
		args.put(DatabaseHelper.USER_COLUMN_POINTS, userpoints + newPoints);
		database.update(DatabaseHelper.TABLE_USER, args,
				DatabaseHelper.USER_COLUMN_ID + " = " + 1, null);
	}

	public void removePoints(int newPoints) {
		int userpoints = 0;
		Cursor cursor = database.query(DatabaseHelper.TABLE_USER,
				new String[] { DatabaseHelper.USER_COLUMN_POINTS }, null, null,
				null, null, null);
		if (cursor.getCount() == 0) {
			createUser();
			cursor = database.query(DatabaseHelper.TABLE_USER,
					new String[] { DatabaseHelper.USER_COLUMN_POINTS }, null,
					null, null, null, null);
		}
		if (cursor.moveToFirst())
			userpoints = cursor.getInt(cursor
					.getColumnIndex(DatabaseHelper.USER_COLUMN_POINTS));
		ContentValues args = new ContentValues();
		args.put(DatabaseHelper.USER_COLUMN_POINTS, userpoints - newPoints);
		database.update(DatabaseHelper.TABLE_USER, args,
				DatabaseHelper.USER_COLUMN_ID + " = " + 1, null);
	}

	public void addFiveTasksDoneBadge() {
		ContentValues args = new ContentValues();
		args.put(DatabaseHelper.USER_COLUMN_BADGES, FIVETASKSDONE_BADGE);
		database.update(DatabaseHelper.TABLE_USER, args,
				DatabaseHelper.USER_COLUMN_ID + " = " + 1, null);
	}

	public Cursor getBadges() {
		Cursor cursor = database.query(DatabaseHelper.TABLE_USER, null,
				DatabaseHelper.USER_COLUMN_ID + " = " + 1, null, null, null,
				null);
		return cursor;
	}

	public int getPoints() {
		Cursor cursor = database.query(DatabaseHelper.TABLE_USER, null,
				DatabaseHelper.USER_COLUMN_ID + " = " + 1, null, null, null,
				null);
		if (cursor.moveToFirst())
			return cursor.getInt(cursor
					.getColumnIndex(DatabaseHelper.USER_COLUMN_POINTS));
		return 0;
	}
}
